<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="摘要信息" prop="UserInformation">
        <el-input
          v-model="queryParams.UserInformation"
          placeholder="请输入摘要信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="userType">
        <el-select
          v-model="queryParams.userType"
          placeholder="用户类型"
          clearable
          style="width: 240px"
        >
            <el-option label="最低级用户" :value="0" />
            <el-option label="低级用户" :value="1" />
            <el-option label="中级用户" :value="2" />
            <el-option label="高级用户" :value="3" />
            <el-option label="最高级用户" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="级别" prop="userLevel">
        <el-select
          v-model="queryParams.status"
          placeholder="用户级别"
          clearable
          style="width: 240px"
        >
            <el-option label="个人" :value="0" />
            <el-option label="私人企业" :value="1" />
            <el-option label="国有重点企业" :value="2" />
            <el-option label="组织机构" :value="3" />
            <el-option label="事业单位" :value="4" />
            <el-option label="普通局委办" :value="5" />
            <el-option label="权力机构" :value="6" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['userInfo:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['userInfo:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['userInfo:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['userInfo:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="UserInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <!-- <el-table-column label="用户ID" align="center" prop="id" /> -->
      <el-table-column label="用户账号" align="center" prop="username" />
      <!-- <el-table-column label="用户密码" align="center" prop="password" /> -->
      <el-table-column
        label="用户昵称(简称)"
        align="center"
        prop="alias"
        width="150"
      />
      <el-table-column label="正式名称" align="center" prop="formalname" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">注册</span>
          <span v-else-if="scope.row.status == 1">审核</span>
          <span v-else-if="scope.row.status == 2">认证</span>
          <span v-else-if="scope.row.status == 3">正常</span>
          <span v-else-if="scope.row.status == 4">注销</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否删除" align="center" prop="isDelete">
        <template slot-scope="scope">
          <span v-if="scope.row.isDelete == 0">已删除</span>
          <span v-else-if="scope.row.isDelete == 1">未删除</span>
          <span v-else>其他</span>
        </template>
      </el-table-column> -->
      <el-table-column label="用户级别" align="center" prop="userLevel">
        <template slot-scope="scope">
          <span v-if="scope.row.userLevel == 0">最低级用户</span>
          <span v-else-if="scope.row.userLevel == 1">低级用户</span>
          <span v-else-if="scope.row.userLevel == 2">中级用户</span>
          <span v-else-if="scope.row.userLevel == 3">高级用户</span>
          <span v-else-if="scope.row.userLevel == 4">最高级用户</span>
        </template>
      </el-table-column>
      <el-table-column label="用户类别" align="center" prop="userType">
        <template slot-scope="scope">
          <span v-if="scope.row.userType == 0">个人</span>
          <span v-else-if="scope.row.userType == 1">私人企业</span>
          <span v-else-if="scope.row.userType == 2">国有重点企业</span>
          <span v-else-if="scope.row.userType == 3">组织机构</span>
          <span v-else-if="scope.row.userType == 4">事业单位</span>
          <span v-else-if="scope.row.userType == 5">普通局委办</span>
          <span v-else-if="scope.row.userType == 6">权力机构</span>
        </template>
      </el-table-column>
      <el-table-column
        label="最近登录IP"
        align="center"
        prop="loginIp"
        width="150"
      />
      <el-table-column
        label="最近登录时间"
        align="center"
        prop="loginDate"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="注册时间"
        align="center"
        prop="registerTime"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="介绍" align="center" prop="osVersion" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-star-off"
            @click="
              operatDialog = !operatDialog;
              operatForm.userId = scope.row.id;
            "
            >辅助操作</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['userInfo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['userInfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改引擎信息信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入用户密码" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="alias">
          <el-input v-model="form.alias" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="正式名称" prop="formalname">
          <el-input v-model="form.formalname" placeholder="请输入正式名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="介绍" prop="remark">
          <el-input type="textarea" v-model="form.remark" :min-height="192" />
        </el-form-item>
        <el-form-item label="用户级别" prop="userLevel">
          <el-select v-model="form.userLevel">
            <el-option label="最低级用户" :value="0" />
            <el-option label="低级用户" :value="1" />
            <el-option label="中级用户" :value="2" />
            <el-option label="高级用户" :value="3" />
            <el-option label="最高级用户" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户类别" prop="userType">
          <el-select v-model="form.userType">
            <el-option label="个人" :value="0" />
            <el-option label="私人企业" :value="1" />
            <el-option label="国有重点企业" :value="2" />
            <el-option label="组织机构" :value="3" />
            <el-option label="事业单位" :value="4" />
            <el-option label="普通局委办" :value="5" />
            <el-option label="权力机构" :value="6" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 辅助操作弹窗 -->
    <el-dialog title="辅助操作" :visible.sync="operatDialog" width="600px">
      <el-form ref="form" :model="operatForm" :rules="rules" label-width="80px">
        <el-form-item label="操作内容" prop="type">
          <el-radio v-model="operatForm.type" :label="0">数据通道注册</el-radio>
          <el-radio v-model="operatForm.type" :label="1">数据产品注册</el-radio>
          <el-radio v-model="operatForm.type" :label="2">定制管理</el-radio>
        </el-form-item>
        <el-form-item label="授权码" prop="code">
          <el-input v-model="operatForm.code" placeholder="请输入授权码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAuxiliary">确 定</el-button>
        <el-button @click="operatDialog = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUserInfo,
  getUserInfo,
  delUserInfo,
  addUserInfo,
  updateUserInfo,
} from "@/api/qysj/app/UserInfo.js";

export default {
  name: "UserInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 按类型搜
      typeSearch: {},
      // 按级别搜
      leavelSearch: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引擎信息信息表格数据
      UserInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //辅助操作弹窗
      operatDialog: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        auditId: null,
        startTime: null,
        endTime: null,
        name: null,
        createUserId: null,
        type: null,
        unit: null,
        price: null,
        status: null,
        userType: null,
        userLevel: null,
      },
      // 表单参数
      form: {},
      //辅助操作表单
      operatForm: {},
      // 表单校验
      rules: {
        auditId: [
          { required: true, message: "审核Id不能为空", trigger: "blur" },
        ],
        createUserId: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询引擎信息信息列表 */
    getList() {
      this.loading = true;
      listUserInfo(this.queryParams).then((response) => {
        this.UserInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        updateTime: null,
        isDelete: 0,
        createBy: null,
        createUserId: null,
        remark: null,
        status: 0,
        username: null,
        password: null,
        alias: null,
        formalname: null,
        contact: null,
        phone: null,
        delFlag: 0,
        loginIp: null,
        loginDate: null,
        registerTime: null,
        userLevel: null,
        userType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUserInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateUserInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form);
            addUserInfo(this.form).then((response) => {
              console.log(response);
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户信息信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delUserInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "userInfo/export",
        {
          ...this.queryParams,
        },
        `UserInfo_${new Date().getTime()}.xlsx`
      );
    },
    //提交辅助
    submitAuxiliary() {
      if (this.operatForm.type || this.operatForm.type == 0) {
        if (this.operatForm.type == 0) {
          this.$router.push("/qysj/data/source/qysj/DatasourceAbstractBase");
        } else if (this.operatForm.type == 1) {
          this.$router.push("/qysj/product/DataProduct");
        } else if (this.operatForm.type == 2) {
          this.$router.push("/qysj/custom/qysj/CustomizeRequest");
        }
      } else {
        this.$modal.msg("请选择将要进行的辅助操作！");
      }
    },
  },
};
</script>
